<?php

/**
 * MeetingModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 4 2011-11-29 12:38:40Z rendong237@gmail.com $
 */
class MeetingModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["mid"] = (int)$value["mid"];
		$v["name"] = $value["name"];
		$v["enname"] = $value["enname"];
		$v["content"] = $value["content"];
		$v["encontent"] = $value["encontent"];
		$v["pubtime"] = date("Y-m-d H:i:s");
		
		$mid = $this->db->insert("meeting", $v, true);
		
		// 插入默认字段
		$this->db->query("insert into mdlist(mid, did,seq) select {$mid},did,seq from dict where did <=20");
	}
	
	/**
	 * 修改
	 */
	public function change($mid, array $value)
	{
		$v = array();
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["enname"]) && $v["enname"] = $value["enname"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["encontent"]) && $v["encontent"] = $value["encontent"];
		isset($value["pubtime"]) && $v["pubtime"] = $value["pubtime"];

		$this->db->update("meeting", $v, 'mid=' . (int)$mid);
	}
	
	/**
	 * 删除
	 */
	public function del($mid)
	{
		$this->db->delete("meeting", 'mid=' .  (int)$mid);
		$this->db->delete("mdlist", 'mid=' .  (int)$mid);
		$this->db->delete("email", 'mid=' .  (int)$mid);
		$this->db->delete("user", 'mid=' .  (int)$mid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from meeting ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from meeting' . $where .' order by mid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAllDict($mid)
	{
	    $list     = $this->db->fetchAll ('select t1.seq as tseq,t2.* from mdlist t1
	    left join dict t2 on t2.did = t1.did
	    where t1.mid="?" order by t1.seq desc, did asc', array($mid));
        
        return $list;
	}
	
	/**
	 * 显示
	 */
	public function show($mid)
	{
		return $this->db->fetchRow("select * from `meeting` where mid=\"?\"", array($mid));
	}
	
	/**
	 * 发送邮件
	 *
	 * @param unknown_type $mid
	 * @param unknown_type $tid
	 * @param unknown_type $stype all, select
	 * @param unknown_type $str_ids
	 */
	public function sendMail($mid, $tid, $stype, $str_ids, $fullurl)
	{
		global $cfg;
		
    	$SystemRoot = $_SERVER['SystemRoot'];
    	$isWin = false;
    	if (preg_match("/windows/i", $SystemRoot) != 0)
    		$isWin = true;
    	
    	$logfile = APP . '/cache/maillog.txt';
    	$phpbin = '';    	
    	global $cfg;
    	foreach ($cfg['php'] as $v){
    		if (file_exists($v)){
    			$phpbin = $v;
    			break;
    		}
    	}
    	
    	// 增加发送次数
    	if ($stype == 'all'){
    		$this->db->update('email', array("send" => "send+1"), "mid=" . $mid);
    	}else{
    		$this->db->update('email', array("send" => "send+1"), "eid in (" . $str_ids . ')');
    	}
    	
    	// 写入文件传输
    	$file = APP . '/cache/tmp_str_mail.txt';
    	file_put_contents($file, $str_ids);
    	
    	// mail.php tid,mid,pid,type,sids
    	if ($isWin){
    		WScript_Shell($phpbin . ' "' . APP . '/shell/mail.php" ' . " {$mid} {$tid} {$stype} \"{$fullurl}\"  >> $logfile");
    	}else 
    		exec('nohup ' . $phpbin . ' "' . APP . '/shell/mail.php" ' . " {$mid} {$tid} {$stype} \"{$fullurl}\" >>{$logfile} 2>&1 &" );
	}
}

// windows 后台执行
function WScript_Shell($str_cmd)
{
    $result = true;
    try{
        $obj_ws = new COM('WScript.Shell');
        $obj_ws->Run('%comspec% /c ' . $str_cmd, 0, false);
    }catch(Exception $e){
        $result = $e->getMessage();
    }
    
    return $result;
}